package thoven.achievement.net.ideapluginachievement.ui

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.openapi.wm.StatusBarWidgetFactory
import com.intellij.util.Consumer
import thoven.achievement.net.ideapluginachievement.service.CodeStatsListener
import thoven.achievement.net.ideapluginachievement.service.CodeStatsService
import thoven.achievement.net.ideapluginachievement.service.RecommendationService
import thoven.achievement.net.ideapluginachievement.service.SettingsService
import java.awt.event.MouseEvent

/**
 * 状态栏小部件工厂
 */
class CodeStatsStatusBarWidgetFactory : StatusBarWidgetFactory {
    
    override fun getId(): String = "CodeStatsWidget"
    
    override fun getDisplayName(): String = "代码统计"
    
    override fun isAvailable(project: Project): Boolean = true
    
    override fun createWidget(project: Project): StatusBarWidget {
        return CodeStatsStatusBarWidget(project)
    }
    
    override fun disposeWidget(widget: StatusBarWidget) {
        // 清理资源
    }
    
    override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true
}

/**
 * 状态栏小部件
 */
class CodeStatsStatusBarWidget(private val project: Project) : StatusBarWidget, StatusBarWidget.TextPresentation {
    
    private var statusBar: StatusBar? = null
    private val codeStatsService = CodeStatsService.getInstance(project)
    
    init {
        // 订阅代码统计变更事件
        project.messageBus.connect().subscribe(CodeStatsListener.TOPIC, object : CodeStatsListener {
            override fun onStatsChanged() {
                // 更新状态栏显示
                statusBar?.updateWidget(ID())
            }
        })
    }
    
    override fun ID(): String = "CodeStatsWidget"
    
    override fun install(statusBar: StatusBar) {
        this.statusBar = statusBar
    }
    
    override fun dispose() {
        statusBar = null
    }
    
    override fun getPresentation(): StatusBarWidget.WidgetPresentation {
        return this
    }
    
    override fun getText(): String {
        return try {
            val aggregatedStats = codeStatsService.getAggregatedStats()
            "📊 今日: +${aggregatedStats.todayAdded} -${aggregatedStats.todayDeleted} | 累计: ${aggregatedStats.totalNet}"
        } catch (e: Exception) {
            "📊 Code Achievement"
        }
    }
    
    override fun getAlignment(): Float = 0.0f
    
    override fun getTooltipText(): String {
        return try {
            val aggregatedStats = codeStatsService.getAggregatedStats()
            val consecutiveDays = codeStatsService.getConsecutiveDays()
            val todayAchievements = codeStatsService.getTodayAchievements()
            
            val tooltip = StringBuilder()
            tooltip.append("<html>")
            tooltip.append("<b>📊 代码统计</b><br/>")
            tooltip.append("<br/>")
            tooltip.append("今日新增: ${aggregatedStats.todayAdded} 行<br/>")
            tooltip.append("今日删除: ${aggregatedStats.todayDeleted} 行<br/>")
            tooltip.append("今日净增: ${aggregatedStats.todayAdded - aggregatedStats.todayDeleted} 行<br/>")
            tooltip.append("<br/>")
            tooltip.append("累计新增: ${aggregatedStats.totalAdded} 行<br/>")
            tooltip.append("累计删除: ${aggregatedStats.totalDeleted} 行<br/>")
            tooltip.append("累计净增: ${aggregatedStats.totalNet} 行<br/>")
            
            if (consecutiveDays > 1) {
                tooltip.append("<br/>")
                tooltip.append("⭐ 连续编码 $consecutiveDays 天<br/>")
            }
            
            if (todayAchievements.isNotEmpty()) {
                tooltip.append("<br/>")
                tooltip.append("<b>🎉 今日成就:</b><br/>")
                todayAchievements.forEach { achievement ->
                    tooltip.append("${achievement.getIcon()} ${achievement.getName()}<br/>")
                }
            }
            
            tooltip.append("<br/>")
            tooltip.append("<i>💡 点击查看详细信息</i>")
            tooltip.append("</html>")
            
            tooltip.toString()
        } catch (e: Exception) {
            "<html><b>📊 Code Achievement</b><br/><i>点击查看详细信息</i></html>"
        }
    }
    
    override fun getClickConsumer(): Consumer<MouseEvent>? {
        return Consumer { _ ->
            // 点击时显示代码统计对话框
            val dialog = CodeStatsDialog(project)
            dialog.show()
        }
    }
} 
